import { Point3d } from "../math/Point3d";
import { ctx } from "../scenes";
export const Equals = (d1, d2, itol = 1) => Math.abs(d1 - d2) < itol
export const isNull = value => value === null;
const isType = function (type) {
  return function (obj) {
    return Object.prototype.toString.call(obj) === '[object ' + type + ']';
  }
};
export const isString = isType('String');
export const isArray = isType('Array');
export const isNumber = isType('Number');
export function isObject(obj) {
  return obj !== null && typeof obj === 'object'
}
export function windowToCanvas(x, y) {
  var bbox = ctx.canvas.getBoundingClientRect();
  return new Point3d(
    x - bbox.left * (ctx.canvas.width / bbox.width),
    y - bbox.top * (ctx.canvas.height / bbox.height)
  );
}
let drawingSurfaceImageData = null;

export function saveDrawingSurface(ctx) {
  drawingSurfaceImageData = ctx.getImageData(0, 0,
    ctx.canvas.width,
    ctx.canvas.height);
}
export function restoreDrawingSurface(ctx) {
  ctx.putImageData(drawingSurfaceImageData, 0, 0);
}

export function proxy(target, sourceKey, key) {
  Object.defineProperty(target, key, {
    get() {
      return target[sourceKey][key]
    },
    set(val) {
      target[sourceKey][key] = val
    }
  })
}